package com.x.attendance.assemble.control.jaxrs.fingerprint;

import com.google.gson.JsonElement;
import com.x.base.core.project.annotation.JaxrsDescribe;
import com.x.base.core.project.annotation.JaxrsMethodDescribe;
import com.x.base.core.project.annotation.JaxrsParameterDescribe;
import com.x.base.core.project.http.ActionResult;
import com.x.base.core.project.http.EffectivePerson;
import com.x.base.core.project.jaxrs.ResponseFactory;
import com.x.base.core.project.jaxrs.StandardJaxrsAction;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.container.AsyncResponse;
import javax.ws.rs.container.Suspended;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;

@Path("fingerprint")
@JaxrsDescribe("指纹管理接口")
public class FingerprintAction extends StandardJaxrsAction {

    private static Logger logger = LoggerFactory.getLogger(FingerprintAction.class);

    @JaxrsMethodDescribe(value = "采集指纹", action = ActionCollectFingerprint.class)
    @POST
    @Path("collect/{employeeId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public void collectFingerprint(@Suspended final AsyncResponse asyncResponse,
                                   @Context HttpServletRequest request,
                                   @JaxrsParameterDescribe("员工ID") @PathParam("employeeId") String employeeId,
                                   JsonElement jsonElement) {
        ActionResult<ActionCollectFingerprint.Wo> result = new ActionResult<>();
        EffectivePerson effectivePerson = this.effectivePerson(request);
        try {
            result = new ActionCollectFingerprint().execute(effectivePerson, employeeId, jsonElement);
        } catch (Exception e) {
            logger.error(e, effectivePerson, request, jsonElement);
            result.error(e);
        }
        asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result));
    }

    @JaxrsMethodDescribe(value = "保存指纹", action = ActionSaveFingerprint.class)
    @POST
    @Path("save")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public void saveFingerprint(@Suspended final AsyncResponse asyncResponse,
                                @Context HttpServletRequest request,
                                JsonElement jsonElement) {
        ActionResult<ActionSaveFingerprint.Wo> result = new ActionResult<>();
        EffectivePerson effectivePerson = this.effectivePerson(request);
        try {
            result = new ActionSaveFingerprint().execute(effectivePerson, jsonElement);
        } catch (Exception e) {
            logger.error(e, effectivePerson, request, jsonElement);
            result.error(e);
        }
        asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result));
    }

    @JaxrsMethodDescribe(value = "获取员工指纹列表", action = ActionListFingerprints.class)
    @POST
    @Path("list/{employeeId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public void listFingerprints(@Suspended final AsyncResponse asyncResponse,
                                 @Context HttpServletRequest request,
                                 @JaxrsParameterDescribe("员工ID") @PathParam("employeeId") String employeeId) {
        ActionResult<ActionListFingerprints.Wo> result = new ActionResult<>();
        EffectivePerson effectivePerson = this.effectivePerson(request);
        try {
            result = new ActionListFingerprints().execute(effectivePerson, employeeId);
        } catch (Exception e) {
            logger.error(e, effectivePerson, request, null);
            result.error(e);
        }
        asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result));
    }

    @JaxrsMethodDescribe(value = "删除指纹", action = ActionDeleteFingerprint.class)
    @POST
    @Path("delete/{fingerprintId}")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public void deleteFingerprint(@Suspended final AsyncResponse asyncResponse,
                                  @Context HttpServletRequest request,
                                  @JaxrsParameterDescribe("指纹ID") @PathParam("fingerprintId") String fingerprintId) {
        ActionResult<ActionDeleteFingerprint.Wo> result = new ActionResult<>();
        EffectivePerson effectivePerson = this.effectivePerson(request);
        try {
            result = new ActionDeleteFingerprint().execute(effectivePerson, fingerprintId);
        } catch (Exception e) {
            logger.error(e, effectivePerson, request, null);
            result.error(e);
        }
        asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result));
    }

    @JaxrsMethodDescribe(value = "初始化指纹采集器", action = ActionInitializeCollector.class)
    @POST
    @Path("initialize")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public void initializeCollector(@Suspended final AsyncResponse asyncResponse,
                                    @Context HttpServletRequest request) {
        ActionResult<ActionInitializeCollector.Wo> result = new ActionResult<>();
        EffectivePerson effectivePerson = this.effectivePerson(request);
        try {
            result = new ActionInitializeCollector().execute(effectivePerson);
        } catch (Exception e) {
            logger.error(e, effectivePerson, request, null);
            result.error(e);
        }
        asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result));
    }

    @JaxrsMethodDescribe(value = "获取采集器状态", action = ActionGetCollectorStatus.class)
    @GET
    @Path("collector/status")
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    public void getCollectorStatus(@Suspended final AsyncResponse asyncResponse,
                                   @Context HttpServletRequest request) {
        ActionResult<ActionGetCollectorStatus.Wo> result = new ActionResult<>();
        EffectivePerson effectivePerson = this.effectivePerson(request);
        try {
            result = new ActionGetCollectorStatus().execute(effectivePerson);
        } catch (Exception e) {
            logger.error(e, effectivePerson, request, null);
            result.error(e);
        }
        asyncResponse.resume(ResponseFactory.getEntityTagActionResultResponse(request, result));
    }
}

